package com.health.db.dao.sm.impl;

import java.util.List;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.sm.IGoodsListDao;
import com.health.db.mapper.GoodsListMapper;
import com.health.db.model.GoodsList;
import com.health.db.model.GoodsListCouse;
import com.health.db.model.GoodsListExample;
import com.health.db.model.GoodsListTemp;
import com.health.redis.RedisConstants;

public class GoodsListDaoImpl extends ServiceInterface implements
		IGoodsListDao {

	GoodsListMapper goodsListMapper;
	
	String  GoodsId=":goodsid:";
	String  Typeid=":typeid:";
	
	String  offset=":OF:";
	String  pageSize=":PS:";
	
	String selectByExample=":1";
	String selectBySimple=":2";
	String selectByGoodsId=":3";
	String selectGoodsAndDatasByGoodsId=":4";
	
	/**
	 * example {
	 *     typeid 类型
	 *     goodsid 商品id
	 * }
	 */
	@Override
	public List<GoodsList> selectByExample(GoodsListExample example) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key=RedisConstants.GoodsListMapper+GoodsId+example.getGoodsid() +Typeid+example.getTypeid()+selectByExample;
		Object object=null;
		 List<GoodsList>  list=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object){
			if (!object.equals("")) {
				list =  (List) object;
			}
		}
		else
		{
			list=goodsListMapper.selectByExample(example);
			if(redisOperatiorLogic.isJedisIsConnected())
			{
			redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}

	/**
	 * goodsId 商品号
	 */
	@Override
	public GoodsList selectByGoodsId(Long goodsId) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key=RedisConstants.GoodsListMapper+GoodsId+goodsId+selectByGoodsId;
		Object object=null;
		GoodsList  list=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object){
			
			if (!object.equals("")) {
				list =  (GoodsList) object;
			}
			
		}
		else
		{
			list=goodsListMapper.selectByGoodsId(goodsId);
			if(redisOperatiorLogic.isJedisIsConnected())
			{
			redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
		
	}

	/**
	 * example {
	 *     typeid 类型
	 *     goodsid 商品id
	 * }
	 */
	@Override
	public List<GoodsListTemp> selectBySimple(GoodsListExample example) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key=RedisConstants.GoodsListMapper+GoodsId+example.getGoodsid() +Typeid+example.getTypeid()+selectBySimple;
		
		if (0!=example.getPageSize()) {
			key=key+offset+example.getOffset()+pageSize+example.getPageSize();
		}
		Object object=null;
		List<GoodsListTemp> list=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object){
			if (!object.equals("")) {
				list = (List) object;
			}
		}
		else
		{
			list=goodsListMapper.selectBySimple(example);
			if(redisOperatiorLogic.isJedisIsConnected())
			{
			redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}
	
	
	/**
	 * example {
	 *     typeid 类型
	 *     goodsid 商品id
	 * }
	 */
	@Override
	public List<GoodsListCouse> selectBySimpleCouse(GoodsListExample example) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key=RedisConstants.GoodsListMapper+"C"+GoodsId+example.getGoodsid() +Typeid+example.getTypeid()+selectBySimple;
		
		if (0!=example.getPageSize()) {
			key=key+offset+example.getOffset()+pageSize+example.getPageSize();
		}
		Object object=null;
		List<GoodsListCouse> list=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object){
			if (!object.equals("")) {
				list = (List) object;
			}
		}
		else
		{
			list=goodsListMapper.selectBySimpleCouse(example);
			if(redisOperatiorLogic.isJedisIsConnected())
			{
			redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}

	/**
	 * goodsId 商品号
	 */
	@Override
	public GoodsList selectGoodsAndDatasByGoodsId(Long goodsId) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key=RedisConstants.GoodsListMapper+GoodsId+goodsId+selectGoodsAndDatasByGoodsId;
		Object object=null;
		GoodsList  list=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object){
			
			if (!object.equals("")) {
				list =  (GoodsList) object;
			}
		}
		else
		{
			list= goodsListMapper.selectGoodsAndDatasByGoodsId(goodsId);
			if(redisOperatiorLogic.isJedisIsConnected())
			{
			redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
		
	}
	/***********************************************************************************************/

	public GoodsListMapper getGoodsListMapper() {
		return goodsListMapper;
	}

	public void setGoodsListMapper(GoodsListMapper goodsListMapper) {
		this.goodsListMapper = goodsListMapper;
	}
	
}
